package com.cangshi.service;

import com.alibaba.fastjson.JSON;
import com.cangshi.dao.ActivityMapper;
import com.cangshi.dao.GroupMapper;
import com.cangshi.entity.*;
import com.cangshi.exception.JSONException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Eoly on 2017/1/16.
 */
@Service("activityService")
public class ActivityServiceImpl implements ActivityService {

    @Autowired
    ActivityMapper activityMapper;

    @Autowired
    GroupMapper groupMapper;

    public JsonObject addActivity(Activity activity, UserBaseInfo loginUser) {

        // 检查用户权限
        Group group = groupMapper.selectByPrimaryKey(activity.getActivityGroupId());

        activity.setActivityId(null);
        activity.setActivityInitTime(null);
        if (activity.getActivityTitle() == null
                && activity.getActivityDetail() == null
                && activity.getActivityGroupId() == null) {
            throw new JSONException(191, "添加活动的参数不完整");
        }
        if (!loginUser.getUserId().equals(group.getGroupInitiatorId())) {
            throw new JSONException(192, "该团非该商家所发！");
        }
        Integer result = activityMapper.insertSelective(activity);

        if (!result.equals(1)) {
            throw new JSONException(192, "添加活动失败");
        }

        return new CorrectJsonObject("添加活动成功");
    }

    public PageInfo<Activity> findActivitiesSimply(Integer page) {
        final int pageSize = 2;
        PageHelper.startPage(page, pageSize);
        List<Activity> activityList = activityMapper.selectActivitiesSimply();
        return new PageInfo<Activity>(activityList);
    }

    public Activity findActivityByActivityId(Integer activityId) {
        Activity activity = activityMapper.selectByPrimaryKey(activityId);
        if (activity == null) {
            throw new JSONException(293, "没有找到该活动");
        }
        return activity;
    }

    public Activity findActivityByGroupId(Integer groupId) {
        return null;
    }

    public List<Activity> findActivitiesSimplyByUserId(Integer userId) {
        return null;
    }

    public JsonObject deleteActivities(Integer[] activityIds) {
        return null;
    }
}
